package com.magina.project.quartz.controller;


import com.magina.project.quartz.base.QuartzScheduler;
import com.magina.project.quartz.entity.ScheduleDataBean;
import com.magina.project.quartz.util.CronUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

/**
 *
 * @author Magina.Hu
 * @since 2019-04-19
 */
@Slf4j
@RestController
@RequestMapping("/sysUser")
public class ScheduleManagerController {

    @Autowired
    private QuartzScheduler quartzScheduler;

    @GetMapping("/start")
    public ResponseEntity start() throws SchedulerException {
        ScheduleDataBean data = new ScheduleDataBean();
        data.setTaskId(UUID.randomUUID().toString());
        data.setGroupId(UUID.randomUUID().toString());

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        log.info("current time : {}",calendar.getTime());
        calendar.add(Calendar.MILLISECOND,10000);
        data.setIconStr(CronUtil.getCron(calendar.getTime()));
        quartzScheduler.setScheduleDataBean(data);
        quartzScheduler.startJob();
        return ResponseEntity.ok("success");
    }

    @GetMapping("/pause")
    public ResponseEntity pause(@RequestParam("name") String name ,@RequestParam("group") String group) throws SchedulerException {
        quartzScheduler.pauseJob(name,group);
        return ResponseEntity.ok("success");
    }

    @GetMapping("/getJobInfo")
    public ResponseEntity getJobInfo(@RequestParam("name") String name ,@RequestParam("group") String group) throws SchedulerException {
        String result = quartzScheduler.getJobInfo(name,group);
        log.info(result);
        return ResponseEntity.ok("success");
    }

    @GetMapping("/resumeJob")
    public ResponseEntity resumeJob(@RequestParam("name") String name ,@RequestParam("group") String group) throws SchedulerException {
         quartzScheduler.resumeJob(name,group);
        return ResponseEntity.ok("success");
    }

    @GetMapping("/deleteJob")
    public ResponseEntity deleteJob(@RequestParam("name") String name ,@RequestParam("group") String group) throws SchedulerException {
        quartzScheduler.deleteJob(name,group);
        return ResponseEntity.ok("success");
    }

}

